import 'package:flutter/foundation.dart';
import 'package:syria_sports/core/app_export.dart';
import 'package:flutter/material.dart';
import 'package:syria_sports/core/utils/show_toast.dart';
import 'package:syria_sports/data/apiClient/apis_data.dart';
import 'package:syria_sports/data/models/auth/user.dart';
import 'package:flutter_slidable/flutter_slidable.dart';
import 'package:syria_sports/presentation/players/add_player_page/bloc/add_player_page_screen.dart';
import 'package:syria_sports/presentation/players/add_player_page/widgets/my_circle_avatar.dart';
import 'package:syria_sports/presentation/admin_home_tabs/cp_tab/bloc/cp_cubit.dart';
import '../../../../data/controllers/users_controller.dart';
import '../../../../dialogs/add_attendance_dialog.dart';
import '../../../../dialogs/delete_player_confirmation.dart';
import '../../player_profile_screen/player_profile_screen.dart';


class PlayerItemView extends StatelessWidget {
  final User user;
  final int index;
  const PlayerItemView({super.key, required this.user, required this.index});

  @override
  Widget build(BuildContext context) {

    return Slidable(
      enabled: PrefUtils.isGymAdmin(),
      // The start action pane is the one at the left or the top side.
      startActionPane: ActionPane(
        // A motion is a widget used to control how the pane animates.
        motion: const ScrollMotion(),

        /*// A pane can dismiss the Slidable.
        dismissible: DismissiblePane(onDismissed: () {}),*/

        // All actions are defined in the children parameter.
        children: [
          // A SlidableAction can have an icon and/or a label.
          SlidableAction(
            onPressed: (context){
              showDeleteConfirmation(context, user);
            },
            backgroundColor: Colors.transparent,
            foregroundColor: Colors.red,
            icon: Icons.delete,
            label: 'حذف',
          ),
          SlidableAction(
            onPressed: (context){
               Navigator.push(context, MaterialPageRoute(builder: (context) => AddPlayerPageScreen(player:user,)));
            },
            backgroundColor: Colors.transparent,
            foregroundColor: Colors.orange,
            icon: Icons.edit,
            label: 'تعديل',
          ),
        ],
      ),
      endActionPane: ActionPane(
        motion: const ScrollMotion(),
        children: [
          SlidableAction(
            onPressed: (context){
              showAddAttendanceDialog(context ,user);
            },
            backgroundColor: Colors.transparent,
            foregroundColor: Colors.orange,
            icon: Icons.pending_actions_rounded,
            label: 'إضافة حضور',
          ),
        ],
      ),
      child: GestureDetector(
        onTap: () {
          Navigator.push(
            context,
            MaterialPageRoute(
              builder: (context) => PlayerProfileScreen.builder(
                context: context,
                user: user,
                index: index
              ),
            ),
          );
        },
        child: Card(
          color: Theme.of(context).colorScheme.secondary,
          child: Padding(
            padding: const EdgeInsets.all(8.0),
            child: Row(
              children: [
                Hero(
                  tag: "player_selected$index",
                  child: MyCircleAvatar(
                    borderColor: Colors.red,
                    backgroundColor:Theme.of(context).colorScheme.onPrimaryContainer,
                    editMode: false,
                    width: 38,
                    activeIconWidth: 22,
                    imageURL: user.image,
                    active: user.activeValue == '1',
                  ),
                ),
                SizedBox(width: 8), // مسافة أفقية بين العناصر
                Expanded(
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text(user.fullName, style: theme.textTheme.titleMedium),
                      Text("المستوى: ${user.level}", style: CustomTextStyles.bodySmallOnPrimary),
                    ],
                  ),
                ),
                CustomImageView(
                  imagePath: ImageConstant.imgArrowLeftBlueGray300,
                  height: 32.adaptSize,
                  width: 32.adaptSize,
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
  void showDeleteConfirmation(BuildContext context, User player) {
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return DeleteConfirmationDialog(
          playerName: player.fullName,
          titleUser: 'اللاعب',
          onConfirm: () {
            UsersController.deleteById(player.userId??'-1');
            if (kDebugMode) {
              print('تم حذف اللاعب ${player.fullName}!');
            }
          },
        );
      },
    );
  }

  void showAddAttendanceDialog(BuildContext context, User player) {
    if(APISData.cpCubit.state is CPLoading){
      showErrorMessage(message: 'يرجى الانتظار ريثما يتم جلب التمارين والنوادي');
      return;
    }
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return AddAttendanceDialog(
          player: player,
        );
      },
    );

  }
}

